package com.javaxiaobear.module.system.mapper;

import static com.javaxiaobear.module.system.domain.table.SysUserPostTableDef.SYS_USER_POST;

import com.javaxiaobear.module.system.domain.SysUserPost;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.query.QueryWrapper;
import java.util.Arrays;
import java.util.List;

/**
 * 用户与岗位关联表 数据层
 *
 * @author javaxiaobear
 */
public interface SysUserPostMapper extends BaseMapper<SysUserPost> {
  /** 通过用户ID删除用户和岗位关联 */
  default int deleteUserPostByUserId(Long userId) {
    QueryWrapper queryWrapper = QueryWrapper.create().where(SYS_USER_POST.USER_ID.eq(userId));
    return deleteByQuery(queryWrapper);
  }

  /** 通过岗位ID查询岗位使用数量 */
  default int countUserPostById(Long postId) {
    QueryWrapper queryWrapper = QueryWrapper.create().where(SYS_USER_POST.POST_ID.eq(postId));
    return Math.toIntExact(selectCountByQuery(queryWrapper));
  }

  /** 批量删除用户和岗位关联 */
  default int deleteUserPost(Long[] ids) {
    if (ids == null || ids.length == 0) {
      return 0;
    }
    QueryWrapper queryWrapper =
        QueryWrapper.create().where(SYS_USER_POST.USER_ID.in(Arrays.asList(ids)));
    return deleteByQuery(queryWrapper);
  }

  /** 批量新增用户岗位信息 */
  default int batchUserPost(List<SysUserPost> userPostList) {
    if (userPostList == null || userPostList.isEmpty()) {
      return 0;
    }
    return insertBatch(userPostList);
  }
}
